<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/9/24
  Time: 9:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>学生展示</title>
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" >
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="deleteuser">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addStu">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
    </div>
</script>

<form class="layui-form" action=""  id="saveUserForm" style="display: none"  lay-filter="updateUserFormFilter">
    <input type="hidden" name="id" >

    <form class="layui-form" action="" >

        <div class="registerbox-title">题目修改</div>
        <div class="layui-form-item">
            <label class="layui-form-label">题目</label>
            <div class="layui-input-inline">
                <input type="text" name="timu"   lay-verify="required" placeholder="请输入题目"autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">答案1</label>
            <div class="layui-input-inline">
                <input type="text" name="daan1"  lay-verify="required" placeholder="请输入答案1"autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">答案2</label>
            <div class="layui-input-inline">
                <input type="text" name="daan2"  lay-verify="required" placeholder="请输入答案2"autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">答案3</label>
            <div class="layui-input-inline">
                <input type="text" name="daan3"   lay-verify="required" placeholder="请输入答案3"autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">答案4</label>
            <div class="layui-input-inline">
                <input type="text" name="daan4" o  lay-verify="required" placeholder="请输入答案4"autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSaveUser">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>

    </form>
    <table class="layui-hide" id="test" lay-filter = "test"></table>
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
    <script src="${pageContext.request.contextPath}/static/jquery-3.6.1.min.js"></script>
    <script>
        $(function(){
            layui.use(['table','layer','form','jquery'], function(){
                var table = layui.table;
                var layer = layui.layer;
                var form = layui.form;
                var $ = layui.jquery;
                var upload = layui.upload
                    , element = layui.element;
                var namestatus=true;

                table.render({
                    // element  元素
                    elem: '#test'
                    ,page:true
                    ,limit:5
                    ,limits:[5,10,15,20]
                    ,toolbar: '#toolbarDemo'
                    ,url:'${pageContext.request.contextPath}/fangfa/queryAll' //  表格了里面所需要的数据就是一个list里面有很多的map、对象
                    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    // 传过来的数据进行处理  laydata
                    // ,parseData: function (res) {
                    //     return {
                    //         "code":res.code,
                    //         "msg":res.msg,
                    //         "count": res.data.total,//从返回的json数据中取出总行数
                    //         "data": res.data.list//返回的json数据有两层，layui不支持多层数据，所以是res.list
                    //     }
                    // }
                    ,cols: [[
                        {type: 'checkbox', fixed: 'left'}
                        // field 后面跟的内容  必须跟后台传过来的数据里面的map 的key保持一致
                        ,{field:'id', title: 'id', sort: true ,width:80 }
                        ,{field:'timu', title: '题目',width:300} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                        ,{field:'daan1', title: '答案'}
                        ,{field:'daan2', title: '答案'}
                        ,{field:'daan3', title: '答案'}
                        ,{field:'daan4', title: '答案'}
                        ,{fixed: 'right', title:'操作', width: 125, minWidth: 125, toolbar: '#barDemo'}
                    ]]
                });



                // 工具栏事件

                //触发单元格工具事件
                //监听行工具事件
                table.on('tool(test)', function(obj){
                    var data = obj.data;
                    console.log(obj)
                    if(obj.event === 'edit'){
                        $(".pass").css("display","none");
                        layer.open({
                            type: 1,
                            area: ['500px', '400px'],
                            offset: '100px',
                            content:$("#saveUserForm"),
                            cancel: function(index, layero){

                                layer.close(index);
                                $("#saveUserForm").hide();
                                return false;
                            }
                        });
                        // 给开关赋值
                        if(data.status==1){
                            data.statusswitch=true;
                        }else{
                            data.statusswitch=false;
                        }

                        form.val('updateUserFormFilter',data);
                        $("#demo1").prop("src",data.imgpath);


                    }
                });




                //提交表单
                // Layui提交表单 监听提交  data代表的是form表单里面的所有的内容
                form.on('submit(formSaveUser)', function(data){
                    if(namestatus){
                        $.ajax({
                            url:"${pageContext.request.contextPath}/fangfa/updept",
                            type:"post",
                            dataType:"JSON",
                            data:data.field,//表单里所有数据
                            success:function (ret){
                                if (ret.data == true){
                                    //关闭所有弹框
                                    layer.closeAll();
                                    $("#saveUserForm").hide();
                                    //刷新页面
                                    $(".layui-laypage-btn").click();
                                }
                            }
                        })
                    }
                    return false;
                });
            });
        })

    </script>

</body>
</html>
